
using AvalonAdmin.BaseViewModel;
using AvalonAdmin.Consts;
using AvalonAdmin.Helpers;
using AvalonAdmin.Views;
using AvalonAdminDto.Sys.AdminDtos;
using AvalonAdminDto.Sys.LoginDtos;
using CommunityToolkit.Mvvm.Input;
using D.Util.UtilCache;
using D.UtilCore.Dtos;
using D.UtilCore.Models;
using Irihi.Avalonia.Shared.Contracts;
using System;

namespace AvalonAdmin.ViewModels
{
    /// <summary>
    /// 登录入参
    /// </summary>
    public class LoginViewModel : Base, IDialogContext
    {
        private Login _loginPage;
        public LoginViewModel(Login loginPage)
        {
            _loginPage = loginPage;
        }

        #region 系统信息

        private string systemTitle = "车联网系统";
        /// <summary>
        /// 标题
        /// </summary>
        public string SystemTitle
        {
            get => systemTitle;
            set => SetProperty(ref systemTitle, value);
        }
        #endregion

        #region 登录入参数据

        private string account = "admins";
        /// <summary>
        /// 账号
        /// </summary>
        public string Account
        {
            get => account;
            set => SetProperty(ref account, value);
        }

        private string password = "123456";
        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get => password;
            set => SetProperty(ref password, value);
        }

        #endregion

        #region 登录命令
        /// <summary>
        /// 登录事件
        /// </summary>
        private RelayCommand _loginCommand;
        /// <summary>
        /// 登录事件
        /// </summary>
        public RelayCommand LoginCommand => _loginCommand ?? (_loginCommand = new RelayCommand(Login));

        /// <summary>
        /// 登录函数
        /// </summary>
        /// <param name="loginVM"></param>
        private async void Login()
        {
            // 基础数据判断
            if (string.IsNullOrEmpty(Account))
            {
                ShowWarning("账号不可为空","",false);
                return;
            }

            if (string.IsNullOrEmpty(Password))
            {
                ShowWarning("密码不可为空","", false);
                return;
            }
          
            // 请求后端接口 
            var res = await HttpHelper<string>.PostAsync<ReqLogin>(ApiUrl.Login, new ReqLogin()
            {
                Account = Account,
                Password = Password
            }, false);

            //  判断结果
            if (res.Code == StatusCodeEnum.Waring || res.Code == StatusCodeEnum.Error)
            {
                ShowWarning(res.Msg,"",false);
            }
            else if (res.Code == StatusCodeEnum.ServerError)
            {
                ShowError("服务器异常","", false);
            }
            else
            {
                // 存储Token信息
                var cache = AutoFacHelper.Instance.Get<ICacheService>();
                cache.Set(CacheKey.AccessToken, res.Data?.ToString());

                // 获取用户信息
                var apiResult = await HttpHelper<RepAdminInfo>.GetAsync($"{ApiUrl.GetAdminInfo}");
                if (apiResult.Code == StatusCodeEnum.OK)
                {
                    // 存储用户信息
                    cache.Set(CacheKey.UserName, apiResult.Data?.NickName);
                    cache.Set(CacheKey.Account, apiResult.Data?.Account);

                    // 初始化首页
                    //var mainWindow = new MainWindow()
                    //{
                    //    DataContext = new MainViewModel()
                    //};
                    //mainWindow.Show();

                    // 登录成功
                    RequestClose?.Invoke(this, true);
                }
                else
                {
                    ShowWarning("获取用户信息失败","", false);
                }
            }
        }

        
        #endregion

        #region 退出命令
        /// <summary>
        /// 登录事件
        /// </summary>
        private RelayCommand _exitCommand;

        public event EventHandler<object?>? RequestClose;

        /// <summary>
        /// 登录事件
        /// </summary>
        public RelayCommand ExitCommand => _exitCommand ?? (_exitCommand = new RelayCommand(() =>
        {
            RequestClose?.Invoke(this, false);
        }));
        public void Close()
        {
            RequestClose?.Invoke(this, false);
        }
        #endregion
    }
}



